package Menu
{
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	
	public final class PageManager extends Sprite
	{
		private var _pages:Dictionary;
		private var _curPage:BasePage;
		private var newPage:BasePage;
		
		public function PageManager()
		{
			init();
		}
		protected function init():void{
			_pages=new Dictionary();
		}
		public function registerPage(page:BasePage):void{
			_pages[(page as Object).constructor]=page;
			page.addEventListener(PageEvent.NEED_PAGE,needPageHandler);//прослушиваем когда ктото запросит страницу
		}
		private function needPageHandler(e:PageEvent):void{
			showPage(e.pageClass);
		}
		public function getPage(pageClass:Class):BasePage{
			return _pages[pageClass];
		}
		public function showPage(pageClass:Class):void{
			getPage(pageClass).start();
			newPage=getPage(pageClass);
			
			if (_curPage){
				_curPage.addEventListener(PageEvent.HIDE_COMPLETE,hideCompleteHandler);
				_curPage.hide();
			}else{
				hideCompleteHandler(new PageEvent(""));
			}
			
		}
		private function hideCompleteHandler(e:PageEvent):void{
			if(_curPage){
				_curPage.removeEventListener(PageEvent.HIDE_COMPLETE,hideCompleteHandler);
			}
			addChild(newPage);
			newPage.show();
			_curPage=newPage;
		}
	}
}